package com.lagou.consumer;

import com.lagou.consumer.resource.ConsumerResource;
import com.lagou.filter.component.TPMonitor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.springframework.beans.BeansException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

@SpringBootApplication
public class DubboConsumerApplication implements ApplicationContextAware {

  private static ApplicationContext APPLICATION_CONTEXT;

  private static ExecutorService threadPoolExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    DubboConsumerApplication.APPLICATION_CONTEXT = applicationContext;
  }

  public static void main(String[] args) {
    SpringApplication.run(DubboConsumerApplication.class, args);

    ConsumerResource bean = APPLICATION_CONTEXT.getBean(ConsumerResource.class);
    TPMonitor.start();
    while (true) {
      for (int i = 0; i <= 2000; i++) {
        threadPoolExecutor.submit(() -> {
          bean.invokeRemoteService();
        });
        try {
          Thread.sleep(5);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
  }

}
